import 'package:flutter/material.dart';
import 'package:flutter_douban/config/Http_service.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/loading/custom_footer.dart';
import 'package:flutter_douban/loading/custom_header.dart';
import 'package:flutter_douban/model/active_big_model.dart';
import 'package:flutter_douban/tool_views/back_nav_btn.dart';
import 'package:flutter_douban/tool_views/nav_title_page.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

import 'home_card_view.dart';
import 'note_list_view.dart';

class NoteListPage extends StatefulWidget {
  final int id;
  const NoteListPage({Key? key, required this.id}) : super(key: key);

  @override
  _NoteListPageState createState() => _NoteListPageState();
}

class _NoteListPageState extends State<NoteListPage> {
  List<ActiveBigModel> _topicTwoList = ActiveBigModelList([]).list;
  ScrollController? _scrollController;
  // 下拉刷新控制器
  EasyRefreshController? _easyRefreshController;

  int page = 0;
  int limit = 10;
  bool hasMore = true;
  bool loading = true;
  bool error = false;
  String? errorMsg;

  /// 获取数据
  Future _getActiveData({bool replace = true}) async {
    try {
      /// 全部数据
      Map<String, dynamic> result = await HttpService.getNoteListData(
          page: page, pageSize: 10, id: widget.id);
      ActiveBigModelList topicTwoModelList =
          ActiveBigModelList.fromJson(result['list']);
      setState(() {
        page++;
        if (replace) {
          if (page == 1) {
            _topicTwoList = topicTwoModelList.list;
          }
        } else {
          _topicTwoList.addAll(topicTwoModelList.list);
        }
      });
    } catch (e) {
      setState(() {
        error = true;
        errorMsg = e.toString();
      });
    } finally {
      setState(() {
        loading = false;
      });
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController = ScrollController();
    _easyRefreshController = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: NavTitlePage(
            titleStr: '全部作品',
          ),
          leading: BackNavBtn(),
        ),
        body: EasyRefresh(
          // 开启首次加载刷新
          firstRefresh: true,
          // 加载时的loading
          // firstRefreshWidget: PageFeedBack(firstRefresh: true).build(),
          // 空视图、异常视图
          // emptyWidget: PageFeedBack(
          //   loading: loading,
          //   error: error,
          //   empty: _songList.isEmpty,
          //   errorMsg: errorMsg,
          //   onErrorTap: () => _easyRefreshController.callRefresh(),
          //   onEmptyTap: () => _easyRefreshController.callRefresh(),
          // ).build(),
          header: CustomHeaderView(),
          footer: CustomFooterView(),
          controller: _easyRefreshController,
          enableControlFinishRefresh: false,
          enableControlFinishLoad: true,
          onRefresh: _getActiveData,
          child: Container(
            padding: EdgeInsets.only(
              left: 6,
              right: 6,
            ),
            color: Colors.grey[100],
            child: Column(
              children: [
                StaggeredGridView.countBuilder(
                  controller: _scrollController,
                  itemCount: _topicTwoList.length,
                  primary: false,
                  shrinkWrap: true,
                  crossAxisCount: 4,
                  mainAxisSpacing: 2.0,
                  crossAxisSpacing: 0.0,
                  itemBuilder: (context, index) => NoteListView(
                    model: _topicTwoList[index],
                  ),
                  staggeredTileBuilder: (index) => StaggeredTile.fit(2),
                ),
                Container(
                  height: 60,
                  child: Center(
                    child: Text(
                      '--  End  --',
                      style: TextStyle(
                          color: AppColors.qianTextColor, fontSize: 12),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ));
  }
}
